<?php
class ControllerAccountActivateRegister extends Controller {
	public function index() {
		$this->load->language('account/success');
		$this->load->language('english');

		$this->document->setTitle($this->language->get('heading_title'));
		$verify = $this->request->get['verify'];
		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/home')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_account'),
			'href' => $this->url->link('account/account', '', 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_success_a'),
			'href' => $this->url->link('account/activateregister').'&verify='.$verify
		);

		$data['heading_title'] = $this->language->get('heading_title_a');

		$this->load->model('account/customer_group');
		$this->load->model('account/customer');
		$customer_group_info = $this->model_account_customer_group->getCustomerGroup($this->config->get('config_customer_group_id'));

		if ($customer_group_info && !$customer_group_info['approval']) {		 
		 $nowtime = time();
		 $customer_code_info = $this->model_account_customer->getUserActivation($verify);
		 if($customer_code_info){
		  if($nowtime>$customer_code_info['activation_code_period']){ //48hour
		   //过期后，重新发送激活邮件
		   $this->document->setTitle($this->language->get('heading_title_a_e'));
		   $data['heading_title'] = $this->language->get('heading_title_a_e');
		   $data['text_message'] = sprintf($this->language->get('text_message_a_e'), $this->url->link('information/contact'));
		  }else{
		   $this->model_account_customer->deleteUserActivation($customer_code_info['customer_id']);
		   // Add to activity log
		   $this->load->model('account/activity');
		   $activity_data = array(
		     'customer_id' => $customer_code_info['customer_id'],
		     'name'        => $customer_code_info['firstname'] . ' ' . $customer_code_info['lastname']
		   );   
		   $this->model_account_activity->addActivity('activate', $activity_data);
		   $data['text_message'] = sprintf($this->language->get('text_message_a'), $this->url->link('information/contact'));
		  }
		 }else{
		  $this->document->setTitle($this->language->get('heading_title_a_failed'));
		  $data['heading_title'] = $this->language->get('heading_title_a_failed');
			$data['text_message'] = sprintf($this->language->get('text_message_a_failed'), $this->url->link('information/contact'));
		 }
		} else {
			$data['text_message'] = sprintf($this->language->get('text_approval'), $this->config->get('config_name'), $this->url->link('information/contact'));
		}

		$data['button_continue'] = $this->language->get('button_continue');

		if ($this->cart->hasProducts()) {
			$data['continue'] = $this->url->link('checkout/cart');
		} else {
			$data['continue'] = $this->url->link('account/account', '', 'SSL');
		}

		$data['column_left'] = $this->load->controller('common/column_left');
		$data['column_right'] = $this->load->controller('common/column_right');
		$data['content_top'] = $this->load->controller('common/content_top');
		$data['content_bottom'] = $this->load->controller('common/content_bottom');
		$data['footer'] = $this->load->controller('common/footer');
		$data['header'] = $this->load->controller('common/header');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
			$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/success.tpl', $data));
		} else {
			$this->response->setOutput($this->load->view('default/template/common/success.tpl', $data));
		}
	}
}
